package com.usian.controller;
import com.usian.pojo.Users;
import com.usian.service.UsersServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("/users")
public class UsersController {
    @Autowired
    private UsersServiceImpl usersService;
//页面跳转
@RequestMapping("/{page}")
public String showPage(@PathVariable String page) {
    return page;
}
    //添加用户
    @PostMapping("/addUser")
    public String add(Users users){
        usersService.addUses(users);
        return "redirect:/users/findAll";
    }
    //登录
    @RequestMapping("/login")
    public String login(Model model, Users users, HttpSession session) {
        Users user = usersService.login(users);
        if(user!=null){
            session.setAttribute("user", user);
            return "redirect:/users/findAll";
        }
        return "login";
    }
    //查寻
    @RequestMapping("/findAll")
    public String findAll(Model model){
     List<Users> list= usersService.findAll();
        for (Users users : list) {
            System.out.println(users);
            System.out.println(users);
            System.out.println(users);
        }
        model.addAttribute("list",list);
        return "list";
    }
    //删除
    @RequestMapping("/delete")
    public String login(Integer id) {
        usersService.delete(id);
        return "redirect:/users/findAll";
    }
    //去修改
    @RequestMapping("/up")
    public String up(Integer id,Model model) {
    Users users=usersService.findOne(id);
        System.out.println(users+"11111111");
        System.out.println(users+"11111111");
        System.out.println(users+"11111111");
        model.addAttribute("user",users);
        return "update";
    }
    //upp
    @PostMapping("/upp")
    public String upp(Users users){
        usersService.upp(users);
        return "redirect:/users/findAll";
    }
}
